package com.easyexcel.demo.write;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.easyexcel.demo.util.TestFileUtil;
import com.easyexcel.demo.write.dataVo.CityExportDTO;
import com.easyexcel.demo.write.dataVo.DemoData;
import com.easyexcel.demo.write.handler.MergeCellWriteHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.List;

/**
 * 写的合并单元格写法
 *
 * @author Jiaju Zhuang
 */
@Slf4j
public class WriteMergeTest {


    /**
     * 合并示例，包括行合并和列合并
     */
    @Test
    public void merge(){
        //使用EasyExcel导出文件
        String fileName = TestFileUtil.getPath() + "merge" + System.currentTimeMillis() + ".xlsx";
        log.info("文件名{}", fileName);
        //设置 Cell 样式
        WriteCellStyle writeCellStyle = new WriteCellStyle();
        //设置垂直对齐方式
        writeCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        //设置水平对齐方式
        writeCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);


        EasyExcel.write(fileName, CityExportDTO.class)
                .sheet(1, "导出文件")
                //注册 CellWriterHandler，实现单元格合并
                .registerWriteHandler(new MergeCellWriteHandler())
                .registerWriteHandler(new HorizontalCellStyleStrategy(null, writeCellStyle))
                .doWrite(initDataList());

    }

    /**
     * 最简单的写
     * <p>
     * 1. 创建excel对应的实体对象 参照{@link DemoData}
     * <p>
     * 2. 直接写即可
     */
    @Test
    public void simpleWrite_1() {
        // 注意 simpleWrite在数据量不大的情况下可以使用（5000以内，具体也要看实际情况），数据量大参照 重复多次写入
        // 写法1 JDK8+
        String fileName = TestFileUtil.getPath() + "mergeBefore" + System.currentTimeMillis() + ".xlsx";
        log.info("文件名{}", fileName);

        // 这里 需要指定写用哪个class去写，然后写到第一个sheet，sheet名字为模板 然后文件流会自动关闭
        EasyExcel.write(fileName, CityExportDTO.class)
                .excelType(ExcelTypeEnum.XLSX)
                .sheet("模板")
                .doWrite(() -> {
                    // 分页查询数据
                    return initDataList();
                });
    }

    public List<CityExportDTO> initDataList() {
        List<CityExportDTO> list = Arrays.asList(
                new CityExportDTO("370100", "济南市", "370100", "济南市", "济南市", "启用", "0", "184"),
                new CityExportDTO("370100", "济南市", "370101", "市直属", "学校1", "启用", "12", "184"),
                new CityExportDTO("370100", "济南市", "370101", "市直属", "学校2", "启用", "22", "184"),
                new CityExportDTO("370100", "济南市", "370102", "历下区", "学校3", "启用", "15", "184"),
                new CityExportDTO("370100", "济南市", "370102", "历下区", "学校4", "未启用", "26", "184"),
                new CityExportDTO("370100", "济南市", "370102", "历下区", "学校5", "未接入", "31", "184"),
                new CityExportDTO("370100", "济南市", "370103", "市中区", "学校6", "未知", "18", "184"),
                new CityExportDTO("370100", "济南市", "370104", "槐荫区", "学校7", "启用", "24", "184"),
                new CityExportDTO("370100", "济南市", "370104", "槐荫区", "学校8", "启用", "15", "184"),
                new CityExportDTO("370100", "济南市", "370104", "槐荫区", "学校9", "未确认", "11", "184"),
                new CityExportDTO("370200", "青岛市", "370200", "青岛市", "青岛市", "启用", "0", "268"),
                new CityExportDTO("370200", "青岛市", "370201", "市直属", "学校10", "未接入", "22", "268"),
                new CityExportDTO("370200", "青岛市", "370201", "市直属", "学校11", "启用", "35", "268"),
                new CityExportDTO("370200", "青岛市", "370201", "市南区", "学校12", "启用", "32", "268"),
                new CityExportDTO("370200", "青岛市", "370201", "市南区", "学校13", "未接入", "25", "268"),
                new CityExportDTO("370200", "青岛市", "370201", "市北区", "学校14", "未接入", "27", "268"),
                new CityExportDTO("370200", "青岛市", "370201", "市北区", "学校15", "启用", "16", "268"),
                new CityExportDTO("370200", "青岛市", "370201", "市北区", "学校16", "未确认", "12", "268"),
                new CityExportDTO("370200", "青岛市", "370201", "黄岛区", "学校17", "未知", "10", "268"),
                new CityExportDTO("370200", "青岛市", "370201", "黄岛区", "学校18", "启用", "24", "268"),
                new CityExportDTO("370200", "青岛市", "370201", "黄岛区", "学校19", "未启用", "33", "268"),
                new CityExportDTO("370200", "青岛市", "370201", "黄岛区", "学校20", "未接入", "32", "268")
        );
        return list;
    }



}
